<?php


namespace app\home\controller\user;


use app\home\controller\Base;
use app\home\service\CodeService;
use app\home\service\TokenService;
use app\home\model\feedback\Feedback as FModel;
use app\lib\api\FeedbackFormValidate;

class Feedback extends Base
{

    protected $model;
    protected $uid;

    /**
     * @param {name}
     * @var mixed
     */
    protected $param;
    protected $offset;
    protected $limit = 10;
    /**
     * Feedback constructor.
     * @param FModel $model
     */
    public function __construct(FModel $model)
    {
        $this->model = $model;
        $this->uid = TokenService::getUserInfoFromToken('u_id');
        $this->param = input('post.');
    }

    /*
     * @param images array 图片
     * @param content string 反馈内容
     * @param user_id int 用户id
     * @return array
     * */

    public function putOpinion(){
        (new FeedbackFormValidate())->goCheck();
        if(is_array($this->uid))  return $this->uid;
        $images = !empty($this->param['images']) ? explode(',',$this->param['images']) : null;
        if(count($images) > 3) return CodeService::IMAGE_COUNT_VERIFICATION;
        $data =[
            'u_id'=>$this->uid,
            'images' =>$this->param['images'],
            'content'=>$this->param['content'],
        ];
        return $this->model->save($data) ? CodeService::response('200','反馈成功') :CodeService::SERVER_ERROR_TRY_AGAIN_LATER;
    }

    /**
     * @return array|int
     */
    public function getFeedbackList(){
        if(is_array($this->uid))  return $this->uid;
        $this->offset  = $this->param['page']-1;
        $this->limit = isset($this->param['limit']) ? $this->param['limit'] : $this->limit;
        $data = $this->model->getList($this->uid,$this->offset * $this->limit, $this->limit);
        return !empty($data) ?  CodeService::response(200,'获取成功',$data) :  CodeService::response(201,'暂无消息');
    }

}